package com.mvpandroiddemo.login;


import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatTextView;

import com.google.android.material.textfield.TextInputEditText;
import com.mvpandroiddemo.R;
import com.mvpandroiddemo.base.BaseActivity;
import com.mvpandroiddemo.home.HomeActivity;
import com.mvpandroiddemo.register.RegisterActivity;
import com.orhanobut.logger.Logger;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity implements LoginControl.ILoginView {

    @BindView(R.id.tiet_login_user_name)
    TextInputEditText tietLoginUserName;
    @BindView(R.id.tiet_login_pwd)
    TextInputEditText tietLoginPwd;
    @BindView(R.id.atv_login_register)
    AppCompatTextView atvLoginRegister;
    @BindView(R.id.abtn_login_login)
    AppCompatButton abtnLoginLogin;

    private LoginPresent loginPresent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        loginPresent = new LoginPresent();
        loginPresent.attachView(this);
        loginPresent.initLocal();

    }


    @Override
    public void initLogin(String name, String pwd) {
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {
            return;
        }
        tietLoginUserName.setText(name);
        tietLoginPwd.setText(pwd);
    }


    @Override
    public String getUserName() {
        Editable editable = tietLoginUserName.getText();
        return editable == null ? "" : editable.toString();
    }

    @Override
    public String getPwd() {
        Editable editable = tietLoginPwd.getText();
        return editable == null ? "" : editable.toString();
    }

    @Override
    public void startMainUI() {
        startActivity(new Intent(this, HomeActivity.class));
    }

    @OnClick({R.id.atv_login_register, R.id.abtn_login_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.atv_login_register:
                startActivity(new Intent(this, RegisterActivity.class));
                break;
            case R.id.abtn_login_login:
                loginPresent.onLogin();
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (loginPresent != null) {
            loginPresent.detachView();
        }
    }

}
